#include <iostream>
#include <sw/redis++/redis.h>
#include <vector>
#include <sw/redis++/redis++.h>
#include <string>
#include <unordered_map>
#include "util.hpp"

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;
using sw::redis::Redis;

void test1(Redis &redis)
{
    cout << "hset and hget" << endl;
    redis.flushall();

    redis.hset("key1", "f1", "111");
    redis.hset("key1", std::make_pair("f2", "233"));
    redis.hset("key1", {
        std::make_pair("f3", "3333"),
        std::make_pair("f4", "444")
    });

    vector<std::pair<string, string>> feilds = {
        std::make_pair("f5", "555"),
        std::make_pair("f6", "666")};
    redis.hset("key1", feilds.begin(), feilds.end());

    auto result = redis.hget("key1", "f2");
    if (result)
        cout << "result: " << result.value() << endl;
    else
        cout << "元素无效" << endl;
}

void test2(Redis &redis)
{
    cout << "hexists and hdel and hlen" << endl;
    redis.flushall();

    redis.hset("key1", "f1", "111");
    redis.hset("key1", std::make_pair("f2", "233"));
    redis.hset("key1", {
        std::make_pair("f3", "3333"),
        std::make_pair("f4", "444")
    });
    vector<std::pair<string, string>> feilds = {
        std::make_pair("f5", "555"),
        std::make_pair("f6", "666")};
    redis.hset("key1", feilds.begin(), feilds.end());

    cout << "bool:" << redis.hexists("key1","f3") << endl;
    cout << "bool:" << redis.hexists("key1","f9") << endl;
    
    cout << "hlen: "<< redis.hlen("key1") << endl;
    
    long long ret1 = redis.hdel("key1","f1");
    cout <<"hdel1: "<< ret1 << endl;

    long long ret2 = redis.hdel("key1",{"f1","f2", "f3"});
    cout <<"hdel2: "<< ret2 << endl;

    cout << "hlen: "<< redis.hlen("key1") << endl;
}

void test3(Redis& redis)
{
    cout << "hkeys and hvals" << endl;
    redis.flushall();

    redis.hset("key1", "f1", "111");
    redis.hset("key1", std::make_pair("f2", "233"));
    redis.hset("key1", {
        std::make_pair("f3", "3333"),
        std::make_pair("f4", "444")
    });
    vector<std::pair<string, string>> feilds = {
        std::make_pair("f5", "555"),
        std::make_pair("f6", "666")};
    redis.hset("key1", feilds.begin(), feilds.end());

    vector<string> keys;
    auto it1 = std::back_inserter(keys);
    redis.hkeys("key1",it1);
    printContiner(keys);

    vector<string> values;
    auto it2 = std::back_inserter(values);
    redis.hvals("key1",it2);
    printContiner(values);
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");

    // test1(redis);
    //test2(redis);
    test3(redis);
    return 0;
}